package com.example.demo.checkReport.service.impl;

import com.example.demo.checkReport.entity.CheckForm;
import com.example.demo.checkReport.entity.CheckHeader;
import com.example.demo.checkReport.entity.PageCheckHeader;
import com.example.demo.checkReport.mapper.CheckFormMapper;
import com.example.demo.checkReport.mapper.CheckHeaderMapper;
import com.example.demo.checkReport.service.CheckHeaderService;
import com.example.demo.checkReport.util.MyExcelUtil;
import com.example.demo.common.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lbyani
 * @since 2021-05-25
 */
@Service
public class CheckHeaderServiceImpl extends ServiceImpl<CheckHeaderMapper, CheckHeader> implements CheckHeaderService {

	@Autowired
	private CheckHeaderMapper checkHeaderMapper;
	
	@Autowired
	private CheckFormMapper checkFormMapper;
	
	/**
	 * 查询所有盘点表表头
	 * @return
	 */
	@Override
	public ResponseData getAllCheckHeader(int currentPage, int pageSize) {
		
		QueryWrapper<CheckHeader> wrapper = new QueryWrapper<CheckHeader>();
		wrapper.orderByDesc("id");
		
		// 根据currentPage 和 PageSize 查询信息
		Page<CheckHeader> page = new Page<CheckHeader>(currentPage,pageSize);
		
		PageCheckHeader lists = new PageCheckHeader();
		try {
			IPage<CheckHeader> checkIpage = checkHeaderMapper.selectPage(page, wrapper);
			
			// 获取所有数据
			lists.setCheckHeaderLists(checkIpage.getRecords());

			for (CheckHeader check:lists.getCheckHeaderLists()) {
				
				String date = check.getCheckDate().substring(0, 10);
				check.setCheckDate(date);
			}
			
			// 获取总页数
			lists.setPagecount(checkIpage.getPages());
			
			// 获取总记录数
			lists.setTotal(checkIpage.getTotal());
			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
			return ResponseData.buildError("查询失败！");
		}
		
		return ResponseData.buildOk(lists);
		
	}
	
	/**
	 * 导出excel盘点表
	 */

	@Override
	public void getAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				String headId = request.getParameter("headId");
//				System.out.println("headerId:"+headerId);
				
				QueryWrapper<CheckForm> wrapper = new QueryWrapper<CheckForm>();
				wrapper.eq("head_id", Integer.valueOf(headId));
				
				List<CheckForm> list = checkFormMapper.selectList(wrapper);
				CheckHeader header = checkHeaderMapper.selectById(Integer.valueOf(headId));
				System.out.println(list);
				System.out.println(header);
				
				HSSFWorkbook workbook = new HSSFWorkbook();
				workbook = MyExcelUtil.getHSSFWorkbook(header,list);  //创建Excel文件(Workbook)*/
				
				Date date =new Date();
				SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
				String time =sdf.format(date);
				/*System.out.println(time);*/
				String[] strs = time.split("-");
				String Time="";
				Time +="物资盘点表"+strs[0]+"年"+strs[1]+"月"+strs[2]+"日"+strs[3]+"："+strs[4];
				System.out.println(Time);
				/*FileOutputStream out = new FileOutputStream(filePath);*/

				response.setHeader("Content-Disposition", "attachment;filename="+new String(Time.getBytes(),"ISO-8859-1")  +".xlsx");
				response.setHeader("Pragma", "no-cache");
				response.setHeader("Cache-Control", "no-cache");
				response.setDateHeader("Expires", 0);
				/*response.setCharacterEncoding("UTF-8");
				response.setContentType("text/html;charset=UTF-8");
				request.setCharacterEncoding("UTF-8");*/
				OutputStream output;
				
				try {
					output = response.getOutputStream();
					BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
					bufferedOutPut.flush();
					workbook.write(bufferedOutPut);
					bufferedOutPut.close();
				}catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("文件导出成功!");
				/*workbook.write(out);//保存Excel文件

				out.close();//关闭文件流
		*/
	}
	

	
}
